/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');


var pfMeetController = {
    getPfMeetPage: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            //模糊查询
            if (req.query.title != undefined) {
                params["title"] = { "$like": "%" + req.query.title + "%" };
            }
            if (req.query.meetNum != undefined) {
                params["meetNum"] = { "$like": "%" + req.query.meetNum + "%" };
            }
            if (req.query.lead != undefined) {
                params["lead"] = { "$like": "%" + req.query.lead + "%" };
            }
            paramT["where"] = params;
            paramT["order"] = [["createTime", 'DESC']];
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            seq.pfMeetModule.findAndCountAll(paramT).then(function (result) {
                res.json({ "success": true, "page": page, "total": result.count, "result": result.rows });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    getPfMeetById: function (req, res) {
        co(function () {
            seq.pfMeetModule.findOne({ where: req.params }).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '查询成功', 'result': qdMeet });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '添加失败' });
            console.log(e);
        })
    },
    addPfMeet: function (req, res) {
        co(function () {
            req.body.id = util.getUUID();
            req.body.createTime = util.getNowTime();
            req.body.meetNum = util.getNowTimeString();
            seq.pfMeetModule.create(req.body).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '添加成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '添加失败' });
            console.log(e);
        })
    },
    updatePfMeet: function (req, res) {
        co(function () {
            seq.pfMeetModule.update(req.body, { where: req.params }).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '修改成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '修改失败' });
            console.log(e);
        })
    },
    deletePfMeet: function (req, res) {
        co(function () {
            var params = req.query.params;
            var arr = params.split(",");
            var paramT = [arr];
            seq.pfMeetModule.destroy({ where: { id: paramT } }).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '删除成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '删除失败' });
            console.log(e);
        })
    }


}
module.exports = pfMeetController;